import logging

import pytest

from resource.pom.gallery import Gallery
from utils.manager import Images


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置图片资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.photo1, app.photo_dir)
    device.mediatool_send(app.photo_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入图片，点击选择一张图片')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        tab_photos = device.get_element_by_key('TabPhotos')
        tab_albums = device.get_element_by_key('TabAlbums')
        device.click_element(tab_photos)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('Gesture_1'))
        logging.info('预期结果1.可以正常进入到图片')
        device.refresh_layout()
        device.assert_key_exist('PhotoBrowserBg')
        device.assert_key_exist('ActionBarButtonBack')
        device.assert_key_exist('PhotoBrowserDateTitle')
        device.assert_key_exist('DetailMenuPanel')
        device.assert_key_exist('ActionBarButtonInfo')
        device.assert_key_exist('ToolBarButtonFavor')
        device.assert_key_exist('ToolBarButtonEdit')
        device.assert_key_exist('ToolBarButtonDelete')
        device.assert_key_exist('ToolBarButtonMore')

        logging.info('步骤2.点击收藏按钮')
        device.click_element(device.get_element_by_key('ToolBarButtonFavor'))
        logging.info('预期结果2.收藏按钮颜色变为蓝色，名称变为取消收藏')
        device.refresh_layout()
        device.assert_text_exist('取消收藏')
        x1, y1, x2, y2 = device.get_element_area(device.get_element_by_key('ToolBarButtonFavor'))
        crop_area = x1 + 72, y1 + 12, x2 - 72, y2 - 36
        favored_pic = device.save_snapshot_to_local('favored.jpeg')
        Images.crop_picture(favored_pic, *crop_area)
        similarity = Images.compare_image_similarity(favored_pic, self.app.favored)
        assert similarity > 0.6, '收藏按钮颜色未变成蓝色'

        logging.info('步骤3.回到相册界面，进入我的收藏查看')
        device.go_back()
        logging.info('预期结果3.选择的图片/视频成功添加进入我的收藏中，我的收藏数量显示对应增加')
        device.refresh_layout()
        device.assert_key_exist('Favor_1')
        after_favor = device.get_element_by_key('Gesture_1')
        snap_area = device.get_element_area(after_favor)
        after_favor_pic = device.save_snapshot_to_local('after_favor.jpeg')
        Images.crop_picture(after_favor_pic, *snap_area)
        similarity = Images.compare_image_similarity(after_favor_pic, self.app.standard_after_favor_pic)
        assert similarity > 0.6, '点击收藏后返回的预览图异常，请查看测试截图进一步确认'
        device.click_element(tab_albums)
        device.refresh_layout()
        device.assert_element_exist(condition={'key': 'AlbumCardDisplayName', 'text': '我的收藏'})

        favor_count = device.get_element_by_key('AlbumCardItemCount', index=1)
        assert int(favor_count.get('text')) == 1, '收藏数量没有增加'
